﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using TeBigScreenWpf.ViewModels;

namespace TeBigScreenWpf.Pages
{
    using System;
    using System.Timers;
    using Timer = System.Timers.Timer;

    /// <summary>
    /// Interaction logic for Page2.xaml
    /// </summary>
    public partial class Page2 : Page
    {
        private class PageState
        {
            public Frame Frame;
            public PageItem Item;
            public bool Animate;
            public Page Current;

            public void Prev()
            {
                var idx = Item.Alternatives.IndexOf(Current);

                if (idx < 0)
                    ChangePage(Item.Alternatives[Item.Alternatives.Count - 1]);
                else if (idx == 0)
                    ChangePage(Item.Page);
                else
                    ChangePage(Item.Alternatives[idx - 1]);
            }

            public void Next()
            {
                var idx = Item.Alternatives.IndexOf(Current);

                if (idx < 0)
                    ChangePage(Item.Alternatives[0]);
                else if (idx == Item.Alternatives.Count - 1)
                    ChangePage(Item.Page);
                else
                    ChangePage(Item.Alternatives[idx + 1]);
            }

            private void ChangePage(Page p)
            {
                Current = p;
                Frame.Navigate(p);
            }
        }

        private Timer mTimer = new Timer();

        private PageItem mItem = new PageItem
        {
            Page = new PageSingle.Page7(),
            Alternatives =
            {
                new PageSingle.Page8(),
                new PageSingle.Page9(),
            }
        };

        private PageState mPageState;

        public Page2()
        {
            InitializeComponent();

            DataContext = vmGlobal.vmPage.Value;

            this.Loaded += Page_Loaded;
            this.Unloaded += Page2_Unloaded;
            

            mPageState = new PageState
            {
                Frame = frame_main,
                Item = mItem,
                Animate = true,
                Current = mItem.Page,
            };

            mPageState.Next();

            mTimer.Interval = 8000;
            mTimer.Elapsed += Timer_Elapsed;
           
        }

        private void Page2_Unloaded(object sender, RoutedEventArgs e)
        {
            mTimer.Stop();
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (mPageState.Animate)
                this.Dispatcher.InvokeAsync(() => mPageState.Next());
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            mTimer.Start();

            //SetAchievement(new[]
            //{
            //    //"破获各类侵犯知识产权案件3601起",
            //    //"破获各类制售伪劣商品犯罪案件2801起",
            //    //"抓获犯罪嫌疑人3407名",
            //    //"摧毁制假窝点1124处",
            //    //"打掉批发犯罪团伙879个",
            //    //"涉案总价值5.6亿元",
            //    "2017年破获刑事案件同比增加9.7%;抓获刑事作案成员同比增加15.4%。",
            //    "杀人、放火等八类案件破案率增至92.5%，同比增加37个百分点，创历史之最。",
            //    "打掉涉黑涉恶团伙数，同比增加64%，抓获经济逃犯全省第一，现行命案和交通肇事亡人逃逸案件实现全破，几项战果三年最好。",
            //    "破获涉案金额200亿元的“4.11”特大虚开增值税专用发票案、全省首例非法买卖车辆档案信息案、建市以来最大毒品案。",
            //    "刑事立案同比下降25.8%，其中，命案下降32%，“两抢一盗”下降34.1%，砸车玻璃、入室盗窃等流窜性、团伙性犯罪，得到了有效遏制。大庆打击预防非法“校园贷”经验，得到推广。",
            //    "据统计，群众安全感由2016年的95.4%，增至98.7%;满意度由2016年的95%，增至97.1%。",
            //    "变两级接警为市局直接派警，警令传到街面、派到警员，使指挥链条缩至最短。",
            //    "以419个四类警务室为支撑，打造“微型派出所”，将工作触角延伸到群众家门口，牢牢将民警固定在一线和群众当中，真正做到了动态布警、快速出警，“1.3.5”快速反应圈的整体控制能力，明显加强。",
            //    "110报警量同比下降21.2%;“立八类”主要案件同比下降12.7%，仅占立案总量的3.22%，双双降至2000年以来最低。",
            //});
        }

        private void SetAchievement(IEnumerable<string> list)
        {
            //foreach (var item in list)
            //{
            //    tb_achievement.Inlines.Add(new Run("◊ "));

            //    tb_achievement.Inlines.Add(new Run(item));

            //    tb_achievement.Inlines.Add(new LineBreak());

            //    tb_achievement.Inlines.Add(new Span
            //    {
            //        FontSize = 4,
            //        Inlines = { new Run("\t") , new LineBreak() }
            //    });
            //}
        }
    }
}